package com.boyu.crm.marketing.coupon.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.boyu.crm.marketing.coupon.domain.bo.StoreCouponBo;
import com.boyu.crm.marketing.coupon.domain.vo.StoreCouponVo;
import com.boyu.crm.marketing.coupon.domain.StoreCoupon;
import com.boyu.crm.marketing.coupon.mapper.StoreCouponMapper;
import com.boyu.crm.marketing.coupon.service.IStoreCouponService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 优惠券Service业务层处理
 *
 * @author xtzyb
 * @date 2023-11-03
 */
@RequiredArgsConstructor
@Service
public class StoreCouponServiceImpl implements IStoreCouponService {

    private final StoreCouponMapper baseMapper;

    /**
     * 查询优惠券
     */
    @Override
    public StoreCouponVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询优惠券列表
     */
    @Override
    public TableDataInfo<StoreCouponVo> queryPageList(StoreCouponBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StoreCoupon> lqw = buildQueryWrapper(bo);
        Page<StoreCouponVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询优惠券列表
     */
    @Override
    public List<StoreCouponVo> queryList(StoreCouponBo bo) {
        LambdaQueryWrapper<StoreCoupon> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<StoreCoupon> buildQueryWrapper(StoreCouponBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StoreCoupon> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), StoreCoupon::getName, bo.getName());
        lqw.eq(bo.getMoney() != null, StoreCoupon::getMoney, bo.getMoney());
        lqw.eq(bo.getIsLimited() != null, StoreCoupon::getIsLimited, bo.getIsLimited());
        lqw.eq(bo.getTotal() != null, StoreCoupon::getTotal, bo.getTotal());
        lqw.eq(bo.getLastTotal() != null, StoreCoupon::getLastTotal, bo.getLastTotal());
        lqw.eq(bo.getUseType() != null, StoreCoupon::getUseType, bo.getUseType());
        lqw.eq(StringUtils.isNotBlank(bo.getPrimaryKey()), StoreCoupon::getPrimaryKey, bo.getPrimaryKey());
        lqw.eq(bo.getMinPrice() != null, StoreCoupon::getMinPrice, bo.getMinPrice());
        lqw.eq(bo.getReceiveStartTime() != null, StoreCoupon::getReceiveStartTime, bo.getReceiveStartTime());
        lqw.eq(bo.getReceiveEndTime() != null, StoreCoupon::getReceiveEndTime, bo.getReceiveEndTime());
        lqw.eq(bo.getIsFixedTime() != null, StoreCoupon::getIsFixedTime, bo.getIsFixedTime());
        lqw.eq(bo.getUseStartTime() != null, StoreCoupon::getUseStartTime, bo.getUseStartTime());
        lqw.eq(bo.getUseEndTime() != null, StoreCoupon::getUseEndTime, bo.getUseEndTime());
        lqw.eq(bo.getDay() != null, StoreCoupon::getDay, bo.getDay());
        lqw.eq(bo.getType() != null, StoreCoupon::getType, bo.getType());
        lqw.eq(bo.getSort() != null, StoreCoupon::getSort, bo.getSort());
        lqw.eq(bo.getStatus() != null, StoreCoupon::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增优惠券
     */
    @Override
    public Boolean insertByBo(StoreCouponBo bo) {
        StoreCoupon add = MapstructUtils.convert(bo, StoreCoupon.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改优惠券
     */
    @Override
    public Boolean updateByBo(StoreCouponBo bo) {
        StoreCoupon update = MapstructUtils.convert(bo, StoreCoupon.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StoreCoupon entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除优惠券
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
